package com.wm.dao.hibernate;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.wm.bean.Comment;
import com.wm.bean.Dish;
import com.wm.bean.Restaurant;
import com.wm.dao.CommentDao;

public class CommentDaoHibernate implements CommentDao{
	@Resource
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	//Query query = sessionFactory.getCurrentSession().createQuery(
	//"from Dish  d where d.rest.rid =  '" + rest.getRid() + "'");
	//List<Dish> dishList = query.list();
	
	@Transactional
	public void save(Comment comment){
		System.out.println("dao");
		sessionFactory.getCurrentSession().save(comment);
	}

	public void update(Comment comment){
		sessionFactory.getCurrentSession().update(comment);
	}


	public void delete(Comment comment){
		sessionFactory.getCurrentSession().delete(comment);
	}
	
	public List<Comment> listComment(String hql){
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	
	public List<Comment> getComments(int beginNum, int maxResults) {
		// sessionFactory.getCurrentSession().createQuery("from
		// Restaurant").list();
		String hql = "from Comment ";
		
		System.out.println(hql);
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setFirstResult(beginNum);
		query.setMaxResults(maxResults);
		List<Comment> cList = query.list();
		System.out.println(cList.get(0).getContent());
		return cList;

	}
	
	public int getCommentPages(int maxResults) {
		int count = 0;
		String hql = "from Comment ";
		
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
//		count = ((Number) query.iterate().next())
//        .intValue();
		count = query.list().size();
		double countPage = count / (maxResults * 1.0);

		int countpage = (int) Math.ceil(countPage);
		System.out.println("总共有" + count + "条记录每页显示" + maxResults + "条总共"
				+ countpage + "页");
		return countpage;
	}

	public int getCommentPagesWithHql(int maxResults, String hql) {
		int count = 0;

		Query query = sessionFactory.getCurrentSession().createQuery(hql);
//		count = ((Number) query.iterate().next())
//        .intValue();
		count = query.list().size();
		double countPage = count / (maxResults * 1.0);

		int countpage = (int) Math.ceil(countPage);
		System.out.println("总共有" + count + "条记录每页显示" + maxResults + "条总共"
				+ countpage + "页");
		return countpage;
	}
	

	public List<Restaurant> getRestaurantsByNameHql(int beginNum, int maxResults, String hql) {
			
//			ArrayList<String> keyWord = (ArrayList<String>)names;
			
			
//			hql = "from Restaurant where 1=1 " + conditionString;
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setFirstResult(beginNum);
			query.setMaxResults(maxResults);
			List<Restaurant> rList = query.list();
//			System.out.println(rList.get(0).getName());
			return rList;

		}

}
